package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.INavService;
import com.cn.wanxi.util.IO;
import com.cn.wanxi.util.Path;
import com.cn.wanxi.util.Tool;

import java.util.Arrays;

public class NavServiceImpl implements INavService {



    @Override
    public NavModel[] getNavModels(String path) {
//        1.創建文件

//        File directory = IO.createDirectory(path, newDirectory);
        IO.createFile(path, Path.newDirectory, Path.FILE_NAME_NAV);
//        2.写入数据
        String[] data = {Arrays.toString(new NavDao().titles), Arrays.toString(new NavDao().hrefs)};
        IO.wireteData(data);
//        3.读取数据
        String result = IO.readData();

//        4.封装数据
        return setData(result);
    }


    private NavModel[] setData(String result) {
        System.out.println(result);
//                NavDao navDao = new NavDao();
        String[] results = result.split(Path.SPLIT);//魔法值
//        String title = results[0];
        String title = Tool.replace(results[0]);
        String href = Tool.replace(results[1]);

        NavModel[] navModels = new NavModel[6];
//        怎么通过result将数据转换为数组titles，hrefs

        String[] titles = title.split(Path.ARRAY_SPLIT);
        String[] hrefs = href.split(Path.ARRAY_SPLIT);
        for (int i = 0; i < navModels.length; i++) {
            NavModel model = new NavModel();
            model.setTitle(titles[i]);
            model.setHref(hrefs[i]);
            navModels[i] = model;
        }
        return navModels;
    }


}
